package ExpressFive;

import javax.swing.*;
import java.awt.*;

//进程，进程无优先级
public class MyProcess {
    private final String id;
    private final Color color;
    private int needTime;
    private final JButton btn;
    private final int time;
    private final int priority;
    MyProcess(String id,int needTime,int priority,Color color){
        this.id=id;
        this.color=color;
        this.needTime=needTime;
        time=this.needTime;
        this.priority=priority;
        btn=new JButton(this.id);
        btn.setBackground(this.color);
        btn.setEnabled(false);
    }
    //时间片轮转，如果运行完成，那么返回
    boolean isRunOver(int time){
        //规定时间小于等于0为结束
        if(needTime<=0)
            return true;//线程结束
        try {
            Thread.sleep(time*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        needTime-=time;
        return needTime <= 0;//线程是否结束
    }
    //获取颜色
    Color getColor() {
        return color;
    }

    public int getTime() {
        return time;
    }

    public int getPriority() {
        return priority;
    }

    public JButton getBtn() {
        return btn;
    }

    @Override
    public String toString() {
        return "MyProcess{" +
                "id='" + id + '\'' +
                ", time=" + time +
                ", priority=" + priority +
                '}';
    }
}
